import getDefaultAuth from "./state";

export function updateAuth({ commit }, auth) {
  auth = auth ?? getDefaultAuth();
  if ("token" in auth) uni.setStorageSync("auth-token", auth.token);
  if ("id" in auth) commit("SET_AUTH_ID", auth.id);
  if ("account" in auth) commit("SET_AUTH_ACCOUNT", auth.account);
  if ("name" in auth) commit("SET_AUTH_NAME", auth.name);
  if ("idCardNo" in auth) commit("SET_AUTH_ID_CARD_NO", auth.idCardNo);
  if ("mobileNo" in auth) commit("SET_AUTH_MOBILE_NO", auth.mobileNo);
  if ("bankCardNo" in auth) commit("SET_AUTH_BANK_CARD_NO", auth.bankCardNo);
}

export function revertAuth({ dispatch }) {
  dispatch("updateAuth", getDefaultAuth());
}
